import ControlScript from "../PublicScript/ControlScript";
import Music, {musicName} from "../PublicScript/Music";
const {ccclass, property} = cc._decorator;

if(CC_BUILD){
   console.log = ()=>{};
}


export enum specialType{
    fluid,
    platform,
    wind,
}

export enum emitType{
    getKey = "getKey",
    cutMan = "cutMan",
    openDoor = "openDoor",
    gameOver = "gameOver",
    overAnim = "overAnim",
    overAnimFollowUp = "overAnimFollowUp",
    changeMoveSide = "changeMoveSide",
    createOverPage = "createOverPage",
    loadOverPage = "loadOverPage",
    showMoney = "showMoney",
    animShowMoney = "animShowMoney",
    changeShowSkin = "changeShowSkin",
    changeMenuSkin = "changeMenuSkin",
    newSkinExper = "newSkinExper",
    resurgence = "resurgence",
    getEggSkin = "getEggSkin",
    eggSkinPage = "eggSkinPage",
}

export enum dataName {
    level = "red_blue_man_level",
    money = "red_blue_man_money",
    skin = "red_blue_man_skin",
    usingSkin = "red_blue_man_usingSkin",
    settingState = "settingState",
    singIn = "singIn",
    giftCount = "giftCount",
    eggCount = "eggCount",
}

@ccclass
export default class GameData_red_blue_man {

    //现在的最高关卡
    static highestLevel = 1;
    //最大关卡数
    static maxLevel = 100;

    //获取数据
    static getGameData(){
        let data = ControlScript.loadData(dataName.level);
        console.log("============================",data, typeof data, data == "");
        if(data == "" || data == undefined || data == null){
            data = 1;
            ControlScript.saveData(dataName.level, data)
        }else{
            data = JSON.parse(data);
        }
        return data;
    }

    //下一关（跳关）
    static nextGame(num?){
        if(num){
            GameData_red_blue_man.changeLevelUnLockList();
            GameData_red_blue_man.highestLevel += num;
        }
        GameData_red_blue_man.saveLevel();
        cc.director.loadScene("GameScene");
    }

    //存关卡数据
    static saveLevel(){
        if(GameData_red_blue_man.highestLevel > GameData_red_blue_man.maxLevel){
            GameData_red_blue_man.highestLevel = 1;
        }
        ControlScript.saveData(dataName.level, GameData_red_blue_man.highestLevel);
    }

    //看视频
    static showVideo(data){

        data.callback();

        if(data.callback1){
            data.callback1();
            return;
        }else{

        }
        ControlScript.createToast("成功观看视频才能获得奖励！")
    }

    //现在的皮肤总数
    static skinSum = 23;
    static skinData = null;
    //获取皮肤数据
    static initSkinData(){
        let skinData = ControlScript.loadData(dataName.skin);
        if(skinData == null || skinData == "" || skinData == undefined){
            ControlScript.loadJson("Data/skinData", (res)=>{
                GameData_red_blue_man.skinData = res.json;
                ControlScript.saveData(dataName.skin, GameData_red_blue_man.skinData);
                this.initLevelUnLockList();
                this.unLockSkinCount();
            })
        }else{
            GameData_red_blue_man.skinData = JSON.parse(skinData);
            this.initLevelUnLockList();
            this.unLockSkinCount();
        }
    }

    //获取皮肤数据
    static getSkinData(){
        return this.skinData;
    }

    //存数据表
    static saveSkinData(){
        this.unLockSkinCount();
        ControlScript.saveData(dataName.skin, GameData_red_blue_man.skinData);
    }

    //获取已解锁皮肤数量
    static unLockSkinCount(){
        GameData_red_blue_man.gotSkinCount = 0;
        for(let i = 0; i < 26; i++){
            if(GameData_red_blue_man.skinData[i].unLock == 1){
                GameData_red_blue_man.gotSkinCount ++;
            }
        }
        GameData_red_blue_man.gotSkinCount -= 1;
        console.log(GameData_red_blue_man.gotSkinCount);
    }

    //
    static usingSkinIndex = 1;
    static initUsingSkinIndex(){
        let index = ControlScript.loadData(dataName.usingSkin);
        if(index == null || index == "" || index == undefined){
            this.usingSkinIndex = 1;
        }else{
            this.usingSkinIndex = parseInt(JSON.parse(index));
        }
        // console.log(this.usingSkinIndex);
    }

    static saveUsingSkinIndex(){
        ControlScript.saveData(dataName.usingSkin, this.usingSkinIndex);
    }

    static getUsingSkinIndex(){
        return this.usingSkinIndex;
    }

    //关卡来解锁皮肤所用到的数组
    static levelUnLockList = {};
    //未解锁的皮肤编号
    static lockSkinNum = [];
    //统计现在以及获得的皮肤数量
    static gotSkinCount = 0;

    //获取数组
    static initLevelUnLockList(){
        for(let i = 0; i < 26; i++){
            if(GameData_red_blue_man.skinData[i].getMethod == "video"){
                if(GameData_red_blue_man.skinData[i].level != "null" && GameData_red_blue_man.skinData[i].unLock == 0) {
                    this.levelUnLockList[GameData_red_blue_man.skinData[i].level] = i;
                }
            }
            if(GameData_red_blue_man.skinData[i].unLock != 1){       //未解锁皮肤
                this.lockSkinNum.push(GameData_red_blue_man.skinData[i].name);
            }
        }
    }

    //修改皮肤解锁值
    static changeLevelUnLockList(){
        if(GameData_red_blue_man.levelUnLockList[GameData_red_blue_man.highestLevel]){
            if(GameData_red_blue_man.skinData[GameData_red_blue_man.levelUnLockList[GameData_red_blue_man.highestLevel]].unLock == 0){
                GameData_red_blue_man.skinData[GameData_red_blue_man.levelUnLockList[GameData_red_blue_man.highestLevel]].unLock = 2;
            }
            GameData_red_blue_man.saveSkinData();
        }
    }

    static settingState = null;
    static initMusic = false;
    static initSettingState(){
        let state = ControlScript.loadData(dataName.settingState);
        if(state == null || state == "" || state == undefined){
            this.settingState = {music: true, effect: true, shake: true, newPlay: true};
            //播放背景音乐
            Music.GamePlayMusic(musicName.bgm);
            console.log("this.settingState:",this.settingState);
        }else{
            this.settingState = JSON.parse(state);
            console.log("this.settingState:",this.settingState);
            if(this.settingState.newPlay == null || this.settingState.newPlay == "undefined"){
                this.settingState.newPlay = true;
                console.log("??????????????????????????");
                this.saveSettingState();
            }
            console.log(this.settingState);
            this.initMusic = !this.settingState.music;
            Music.GamePlayMusic(musicName.bgm);
        }
    }

    static saveSettingState(){
        ControlScript.saveData(dataName.settingState, this.settingState);
    }

    //存水流
    static fluidStateList = [];
    //存移动板
    static moveSideStateList = [];
    //存电扇
    static windStateList = [];

    //存储皮肤礼品的次数对象
    static giftCount = {};

    //初始化次数
    static initGiftCount(){
        let count = ControlScript.loadData(dataName.giftCount);
        if(count != null && count != undefined && count != ""){
            this.giftCount = JSON.parse(count);
        }
    }

    //存gift值
    static saveGiftCount(){
        ControlScript.saveData(dataName.giftCount, this.giftCount)
    }

    //获取渠道（皮肤获取）
    static channelid =  null;

    static initChannelid(){
        ControlScript.loadJson("Data/channelid", (res)=>{
            GameData_red_blue_man.channelid = res.json;
            console.log(GameData_red_blue_man.channelid);
        })
    }

    //彩蛋的获取次数存储
    static eggCount = {};

    static initEggCount(){
        let count = ControlScript.loadData(dataName.eggCount);
        if(count != null && count != undefined && count != ""){
            this.eggCount = JSON.parse(count);
        }
    }

    static saveEggCount(){
        ControlScript.saveData(dataName.eggCount, this.eggCount);
    }

    static levelGuide = null;
    //获取每关的指导
    static initGuide(){
        ControlScript.loadJson("Data/guide", (res)=>{
            this.levelGuide = res.json;
            // console.log("GameData.levelGuide:", GameData.levelGuide);
        })
    }

    //获取
    static getGuide(){
        return this.levelGuide;
    }

    //是否再指导中
    static isGuiding = false;

    static giftNodeList = [];
    static giftNodeCount = 0;
}
